package yf.mall.dysdk.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yf.utils.HttpTookit;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import yf.mall.dysdk.config.UrlConfig;
import yf.mall.dysdk.model.*;
import yf.mall.dysdk.model.common.DyResponse;
import yf.mall.dysdk.model.product.DyProduct;
import yf.mall.dysdk.model.product.DyProductList;
import yf.mall.dysdk.model.product.ProductInfo;
import yf.mall.dysdk.model.product.StoreAddResponse;
import yf.mall.dysdk.model.request.DyKolOrderRequest;
import yf.mall.dysdk.model.request.DyProductSearchRequest;
import yf.mall.dysdk.service.DyAllianceApiService;
import java.util.*;
import java.util.stream.Collectors;

import static yf.mall.common.Const.TK_DOMAIN;

@Slf4j
@Service
public class DyAllianceApiServiceImpl implements DyAllianceApiService {


    @Override
    public DyKolOrderList getKolOrders(DyToken dyToken, DyKolOrderRequest request) {
        String requestJson = JSON.toJSONString(request);
        String responseStr = postJsonWithAccessToken(UrlConfig.ALLIANCE_KOL_ORDERS_URL, dyToken, requestJson);
        DyResponse<DyKolOrderList> response = JSON.parseObject(responseStr, new TypeReference<DyResponse<DyKolOrderList>>() {
        });
        return response.getData();
    }



    @Override
    public StoreAddResponse storeAdd(DyToken dyToken, List<Long> productIds, boolean needHide, String pickExtra, boolean keepPickSource) {
        Map<String, Object> param = new HashMap<>();
        param.put("products", productIds.stream().map(id -> new ProductInfo(id, null)).collect(Collectors.toList()));
        param.put("need_hide", needHide);
        param.put("pick_extra", pickExtra);
        param.put("keep_picksource", keepPickSource);
        String responseStr = postJsonWithAccessToken(UrlConfig.ALLIANCE_STORE_ADD_URL, dyToken, JSON.toJSONString(param));
        DyResponse<StoreAddResponse> response = JSON.parseObject(responseStr, new TypeReference<DyResponse<StoreAddResponse>>() {
        });
        return response.getData();
    }







    @Override
    public DyProductList productsSearch(DyToken dyToken, DyProductSearchRequest request) {
        String requestJson = JSON.toJSONString(request);
        String responseStr = postJsonWithAccessToken(UrlConfig.ALLIANCE_KOL_MATERIALS_PRODUCTS_SEARCH_URL, dyToken, requestJson);
        DyResponse<DyProductList> dyResponse = JSON.parseObject(responseStr, new TypeReference<DyResponse<DyProductList>>() {
        });
        if (dyResponse == null || dyResponse.getData() == null) {
            return new DyProductList(new ArrayList<>(), 0, 0, "");
        }
        DyProductList dyProductList = dyResponse.getData();
        if (dyProductList == null || dyProductList.getErrorCode() != 0) {
            return new DyProductList(new ArrayList<>(), 0, 0, "");
        }
        return dyProductList;
    }

    @Override
    public List<DyProduct> productDetail(DyToken dyToken, List<Long> productIds, boolean withShareStatus, boolean withBrandInfo, boolean withDetailBrief) {
        if (CollectionUtils.isEmpty(productIds)) {
            return Collections.emptyList();
        }
        Map<String, Object> param = new HashMap<>();
        param.put("product_ids", productIds);
        param.put("with_share_status", withShareStatus);
        param.put("with_brand_info", withBrandInfo);
        param.put("with_detail_brief", withDetailBrief);
        String responseStr = postJsonWithAccessToken(UrlConfig.ALLIANCE_KOL_MATERIALS_PRODUCTS_DETAILS_URL, dyToken, JSON.toJSONString(param));
        DyResponse<DyProductList> dyResponse = JSON.parseObject(responseStr, new TypeReference<DyResponse<DyProductList>>() {
        });
        if (dyResponse == null || dyResponse.getData() == null) {
            return Collections.emptyList();
        }
        DyProductList dyProductList = dyResponse.getData();
        if (dyProductList == null || dyProductList.getErrorCode() != 0 || dyProductList.getProducts() == null) {
            return Collections.emptyList();
        }
        return dyProductList.getProducts();
    }




    private String postJsonWithAccessToken(String url, DyToken dyToken, String param) {
        return postJsonWithAccessToken(url, dyToken.getAccessToken(), dyToken.getOpenId(), param);
    }

    private String postJsonWithAccessToken(String url, String token, String openId, String param) {
        Map map = new HashMap();
        map.put("token", token);
        map.put("openId", openId);
        map.put("param", param);
        map.put("url", url);
        return HttpTookit.doGet(TK_DOMAIN+"postJsonWithAccessToken",map);
    }


}
